// pages/index/index
const app = getApp()
const api = require('../../api.js');
const post = api.post;
const getCurrentPageUrlWithArgs = api.getCurrentPageUrlWithArgs;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        paly: [{
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/1.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/2.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/3.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/4.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/5.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/6.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/7.png'
            },
            {
                palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/8.png'
            }
        ],
        seel: '',
        bhyx: ['超级玛丽', 'kirby`s', '松鼠大作战', '魂斗罗', '快打旋风', '雪人兄弟', '三目童子', '赤色要塞']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let uid = wx.getStorageSync('uid');
        let that = this;
        let p = {
            m: 'allowplay',
            i: 1
        }
        post(p).then(res => {
            if (res.r == 0) {
                // console.log(res);
                let allowplay = res.allowplay;
                that.setData({
                    allowplay: res.allowplay
                })
                wx.setStorageSync('allowplay', allowplay);
                let kgame = wx.getStorageSync('kgame')
                var ifGAme
                if (!!kgame) {
                    // console.log(allowplay);
                    if (allowplay == 1) {
                        ifGAme = true;
                    } else {
                        ifGAme = false
                    }
                    that.ifJump(ifGAme)
                } else {
                    wx.redirectTo({
                        url: '../palyGame/palyGame',
                        success: (result) => {}
                    });
                }

            }
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    ifJump(ifGAme) {
        // console.log(ifGAme);
        if (ifGAme) {
            // console.log(ifGAme);
            wx.redirectTo({
                url: '../palyGame/palyGame',
            });
        } else {
            var weapp_openid = wx.getStorageSync('weapp_openid');
            if (!!weapp_openid) {
                api.ifwj(weapp_openid)
            }
        }
    },
    listAll() {
        let paly = [{
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/1.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/2.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/3.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/4.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/5.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/6.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/7.png'
        }, {
            palyImg: 'https://weapp.whwangdoudou.cn/g/palyGame/images/8.png'
        }]
        this.setData({
            paly: paly
        })

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        var ifGAme
        if (that.data.allowplay == 1) {
            ifGAme = true;
        } else {
            ifGAme = false
        }
        that.ifJump(ifGAme)
    },
    bindKeyInput(e) {
        let that = this;
        that.setData({
            seel: e.detail.value
        })
    },
    seel() {
        let that = this;
        var reg = RegExp(that.data.seel);
        if (reg.exec(that.data.bhyx)) {
            if ('超级玛丽' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=1',
                });
            } else if ('kirby`s' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=2',
                });
            } else if ('松鼠大作战' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=3',
                });
            } else if ('魂斗罗' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=4',
                });
            } else if ('快打旋风' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=5',
                });
            } else if ('雪人兄弟' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=6',
                });
            } else if ('三目童子' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=7',
                });
            } else if ('赤色要塞' == that.data.seel) {
                wx.navigateTo({
                    url: '/pages/details/details?paly=8',
                });
            }
        } else {
            wx.showModal({
                title: '提示',
                content: '该游戏攻略还未录入，敬请期待…',
                showCancel: false,
            });
        }

        that.data.bhyx
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // },
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        let page = getCurrentPageUrlWithArgs();
        let that = this;
        let p = {
            m: 'colform',
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId
        };
        if (page && page.length > 0) {
            p.page = page;
        }
        var uid = wx.getStorageSync('uid')
        if (!!uid) {
            p.uid = uid
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        post(p).then(res => {})
    }
})